jclass={
    initPage:function(){
        // 绑定dom对象
        this.obj = {
            searchText:$('#searchText'),
        };
        // 声明全局变量
        this.data = {
            datatable:[],
            tableParams: {
                page:1,
                size:10
            },
            addDialogParams: {
                alarmTypeCode:'',
                alarmType: '',
                alarmLevel: '2',
                industryType: '1',
                delayedAcquisitionTime: '',
                isSwitch:1
            },
            zTree: {},
            zNodes: [
                { id:55, pId:0, name:"随意勾选 1", open:true},
                { id:11, pId:10, name:"随意勾选 1-1", open:true},
                { id:111, pId:11, name:"随意勾选 1-1-1"},
                { id:112, pId:11, name:"随意勾选 1-1-2"},
                { id:12, pId:1, name:"随意勾选 1-2", open:true},
                { id:121, pId:12, name:"随意勾选 1-2-1"},
                { id:122, pId:12, name:"随意勾选 1-2-2"},
                { id:2, pId:0, name:"随意勾选 2", open:true},
                { id:21, pId:2, name:"随意勾选 2-1"},
                { id:22, pId:2, name:"随意勾选 2-2", open:true},
                { id:221, pId:22, name:"随意勾选 2-2-1"},
                { id:222, pId:22, name:"随意勾选 2-2-2"},
                { id:23, pId:2, name:"随意勾选 2-3"}
            ],
            selectHyTypeArr:[{val:'1',name:'班线客运'},{val:'2',name:'旅游包车'},{val:'3',name:'危险品运输'}],
        };
        this.getTableList();
        // this.creatTable({data:{},total:0});
        this.init();
        this.bindEvent();
    },
    init() {
        let that = this

        // 重置
        $("#resetClick").click(() => {
            this.data.tableParams = {
                page:1,
                size:10
            }
        })

        // 查询
        $("#searchClick").click(() => {
            this.getTableList()
        })
        this.initTree()

        jdSelect.JdSelectRadioInit('#dselect_hyType',this.data.selectHyTypeArr,(select) => {
            this.data.addDialogParams.industryType = select.val
        })
        
    },
    // 弹框中的报警类型tree
    initTree(checkedstr) {
        checkedstr = checkedstr || ""
        let checkedarr = []
        if (checkedstr) checkedarr = checkedstr.split(",")
        $(".alarm-tree").hide()
        jrequest.get("/api/alarm-infos/selectAlarmTypeAll",{}).then(res => {
            console.log(res)
            jdtree.init(res.data)
            // res.data.forEach(item => {
            //     if (!item.parentId) item.open = true
            //     if (checkedarr) {
            //         checkedarr.forEach(checkedcode => {
            //             if (checkedcode === item.code) item.checked = true
            //         })
            //     }
            //     item.id = item.nodeId
            //     item.pId = item.parentId
            // })
            // console.log(res.data)
            // let that = this
            // $.fn.zTree.init($("#treeDemo"), {
            //     view: {
            //         showIcon: false,
            //         showLine: false
            //     },
            //     check: {
            //         enable: true
            //     },
            //     data: {
            //         simpleData: {
            //             enable: true
            //         }
            //     },
            //     callback: {
            //         onCheck: (e, treeId, treeNode) => {
            //             // let nodes = this.data.zTree.getChangeCheckedNodes();
            //             let nodes = this.data.zTree.getCheckedNodes();
            //             // console.log(nodes)
            //             let str = ''
            //             let code = ''
            //             nodes.forEach(item => {
            //                 str += `${item.name},`
            //                 code += `${item.code},`
            //             })
            //             console.log(str)
            //             $("#alarmvar").text(str)
            //             that.data.addDialogParams.alarmType = str.slice(0,-1)
            //             that.data.addDialogParams.alarmTypeCode = code.slice(0,-1)
            //         }
            //     }
            // },res.data);
            // this.setCheck();
        });
    },
    setCheck() {
        this.data.zTree = $.fn.zTree.getZTreeObj("treeDemo");
        this.data.zTree.setting.check.chkboxType = {Y: "s", N: "s"};
    },
    bindEvent() {

        $("#add_btn_submit").click(() => {
            if (!$("#alarmvar").text()) {
                // Jdialog.alert("请选择一个报警类型")
                JPop.alert({msg:'请选择一个报警类型'})
                return false
            } else {
                let reg = /^[1-9]\d*$/
                if (!reg.test($("#d_input_timejg").val())) {
                    JPop.alert({msg:'时间必须大于0',type:'error'})
                    return false
                }
                this.data.addDialogParams.alarmTypeCode = jdtree.getCheckIds()
                this.data.addDialogParams.alarmType = jdtree.getCheckNames()
                let dataform = $("#form_addget").serializeArray()
                let a = true
                console.log(dataform)
                dataform.forEach(item => {
                    if(!item.value) a=false
                })
                if(a) {
                    this.data.addDialogParams.delayedAcquisitionTime = $("#d_input_timejg").val()
                    // this.data.addDialogParams.industryType = $("#hytype option:selected").val()
                    if($("#jdialog_title").text() === '编辑') {
                        jrequest.put("/api/alarm_attachments",this.data.addDialogParams).then(res => {
                            if (res) {
                                JPop.alert({msg:'修改成功！',type:'success'})
                                closeDialog()
                                this.getTableList()
                            }
                        })
                    } else {
                        this.data.addDialogParams.id = ''
                        jrequest.post("/api/alarm_attachments",this.data.addDialogParams).then(res => {
                            if (res) {
                                JPop.alert({msg:'添加成功！',type:'success'})
                                closeDialog()
                                this.getTableList()
                            }
                            
                        })
                    }
                    return false
                    
                }
            }

            
            
        })

        $("#jselectMore #selectimgdiv").click(() => {
            $(".alarm-tree").toggle();
        })
    },
    getTableList() {
        jrequest.get("/api/alarm_attachments",this.data.tableParams).then(res => {
            res.data.forEach(item => {
                item.isSwitchName = item.isSwitch == 0 ? '开启' : '关闭'
                item.alarmLevelName = '二级'
            })
            this.data.datatable = res.data
            var height = $(".table-con").height()-180;
            $('#jtable_autoget').bootstrapTable('destroy');
            this.creatTable(res,height)
            // $("#jtable_autoget").bootstrapTable('load',res.data);
        })
    },
    creatTable:function(res,height){
        var self=this;
        var columnsOne = [
            {
                title : '序号',
                align: "center",
                formatter: function (value, row, index) {
                    //获取每页显示的数量
                    var pageSize=self.data.tableParams.size;
                    //获取当前是第几页
                    var pageNumber=self.data.tableParams.page;
                    //返回序号，注意index是从0开始的，所以要加上1
                    return pageSize * (pageNumber - 1) + index + 1;
                }
            },
            {
              field: "alarmType",
              title: "报警类型"
            },
            {
              field: "alarmLevelName",
              title: "报警等级"
            },
            {
              field: "industryType",
              title: "行业类型"
            },
            {
              field: "delayedAcquisitionTime",
              title: "获取证据间隔时间"
            },
            {
              field: "isSwitchName",
              title: "状态"
            },
            {
              field: "founder",
              title: "创建用户"
            },
            {
              field: "createTime",
              title: "创建时间"
            },
            {
                field: 'Button',
                title: '操作',
                width: "200",
                align: 'center',
                events: operateEvents,
            }
        ];
        self.tableEvent("jtable_autoget",res.data,columnsOne,true,res.total,height);
    },
    tableEvent:function(id,data,columns,paginationShow,total,height){
        data.forEach(item => {
            item.Button = this.operateFormatter(item.isSwitch)
        })
        let that = this
        $('#' + id).bootstrapTable({
            method: 'get',
            cache: false,
            height: height,
            pagination: paginationShow,
            pageSize: that.data.tableParams.size,
            pageNumber:that.data.tableParams.page,
            striped: true,
            columns: columns,
            sidePagination: 'server', // client or server
            totalRows: total,
            data :data,
            onPageChange: function (size, number) {
                console.log(number)
                that.data.tableParams.page = number
                that.data.tableParams.size = size
                that.getTableList()
            },
            formatNoMatches: function(){
                return '暂无数据';
            }
        });
    },
    operateFormatter(isSwitch) {
        if (isSwitch == 0) { // 开启状态
            return [
                '<button type="button" id="table_edit">修改</button>',
                '<button type="button" id="table_close">关闭</button>'
            ].join('');
        } else { // 关闭状态
            return [
                '<button type="button" id="table_edit">修改</button>',
                '<button type="button" id="table_close">开启</button>'
            ].join('');
        }
        
    }
    
}

$(function(){
    jclass.initPage();

    // 表格操作按钮
    window.operateEvents = {
        'click #table_edit': (e,value,row,index) => {
            // console.log(row)
            openDialog("edit")
            $("#d_input_timejg").val(row.delayedAcquisitionTime)
            // $("#hytype").find(`option[value=${row.industryTypeCode}]`).attr("selected",true)
            jdSelect.setSelectRadio("#dselect_hyType",jclass.data.selectHyTypeArr,row.industryTypeCode)
            $("#alarmvar").text(row.alarmType)

            jclass.data.addDialogParams.id = row.id
            jclass.data.addDialogParams.alarmType = row.alarmType
            jclass.data.addDialogParams.alarmTypeCode = row.alarmTypeCode
            // jclass.initTree(row.alarmTypeCode)
            jdtree.setChecked(row.alarmTypeCode);
            jclass.data.addDialogParams.industryType = row.industryTypeCode
        },
        'click #table_close': (e,value,row,index) => {
            // console.log(row)
            let msg = ''
            if (row.isSwitch == 0) {
                msg = "确定要关闭自动获取证据？"
            } else {
                msg = "确定要开启自动获取证据？"
            }
            // Jdialog.confirm({ message: msg }).on(function (e) {
            //     if (e) {
            //         // console.log(row.id)
            //         let isSwitch = 0
            //         if (row.isSwitch == 0) {
            //             isSwitch = 1
            //         } else {
            //             isSwitch = 0
            //         }
            //         jrequest.put(`/api/alarm_attachments`,{id:row.id,isSwitch:isSwitch,alarmTypeCode:row.alarmTypeCode,industryType:row.industryTypeCode}).then(res => {
            //             if (res) {
            //                 jclass.getTableList()
            //             }
            //         })
            //     }
            // });
            JPop.dialogDel({msg:msg,operate:true}).on(success => {
                if (success) {
                    let isSwitch = 0
                    if (row.isSwitch == 0) {
                        isSwitch = 1
                    } else {
                        isSwitch = 0
                    }
                    jrequest.put(`/api/alarm_attachments`,{id:row.id,isSwitch:isSwitch,alarmTypeCode:row.alarmTypeCode,industryType:row.industryTypeCode,delayedAcquisitionTime:row.delayedAcquisitionTime}).then(res => {
                        if (res) {
                            jclass.getTableList()
                        }
                    })
                }
            })
        }
    }
});
function timeBlurHandler(e) {
    let val = $("#d_input_timejg").val()
    let reg = /^[1-9]\d*$/
    if (!reg.test(val)) {
        JPop.alert({msg:'时间必须大于0',type:'error'})
    }
}

function openDialog(type) {
    type = type || ''
    if (type === 'edit') {
        $("#jdialog_title").text("编辑")
    } else {
        $("#jdialog_title").text("添加")
    }
    $("#autodbDialog").show()
    $(".alarm-tree").hide();
}
function closeDialog() {
    $("#d_input_timejg").val('')
    // $("#hytype").prop("selectedIndex", 0)
    jdSelect.resetSelectRadio("#dselect_hyType",jclass.data.selectHyTypeArr[0].name)
    $("#alarmvar").text("")
    jdtree.setReset()

    jclass.data.addDialogParams = {
        alarmTypeCode:'',
        alarmType: '',
        alarmLevel: '2',
        industryType: '1',
        delayedAcquisitionTime: '',
        isSwitch:1
    }

    $("#autodbDialog").hide()
}


